package com.heima.comment.listen;

import com.alibaba.fastjson.JSON;
import com.heima.comment.service.CommentService;
import com.heima.common.constants.CommentConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 监听自媒体微服务发过来的kafka消息，删除评论
 */
@Component
public class CommentListener {
    @Autowired
    private CommentService commentService;

    @KafkaListener(topics = CommentConstants.DEL_COMMENT)
    public void onMessage(String msg){
        if (StringUtils.isNotBlank(msg)) {
            Long articleId = JSON.parseObject(JSON.toJSONString(msg), Long.class);
            commentService.delComment(articleId);
        }
    }
}
